//
//  DHSale_GoodShopCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/11.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSale_GoodShopCell.h"
#import "DHBrandProductCollectionCell.h"
#import "DHS2_ProductDetailController.h"
#import "DHS2_BrandViewController.h"

@interface DHSale_GoodShopCell()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UIImageView *coverImgView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@property (weak, nonatomic) IBOutlet UIButton *likeButton;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (assign, nonatomic) BOOL isLiked;
@property (strong, nonatomic) NSArray *dataArray;
@property (strong, nonatomic) NSString *brandID;

@end

@implementation DHSale_GoodShopCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"DHBrandProductCollectionCell" bundle:nil] forCellWithReuseIdentifier:kDHBrandProductCollectionCell];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.likeButton bk_addEventHandler:^(id sender) {
        if (self.isLiked) {
            [self.likeButton setImage:[UIImage imageNamed:@"product_unlike"] forState:UIControlStateNormal];
            [[DHNetAPIClient shareDHNetAPIClient] request_brand_collectWithParams:@{@"brand_id":self.brandID,@"collect":[NSNumber numberWithBool:!self.isLiked]} andBlock:^(id data, NSError *error) {
                self.isLiked = !self.isLiked;
            }];
        }else{
            [self.likeButton setImage:[UIImage imageNamed:@"product_like"] forState:UIControlStateNormal];
            [[DHNetAPIClient shareDHNetAPIClient] request_brand_collectWithParams:@{@"brand_id":self.brandID,@"collect":[NSNumber numberWithBool:!self.isLiked]} andBlock:^(id data, NSError *error) {
                self.isLiked = !self.isLiked;
            }];
        }
    } forControlEvents:UIControlEventTouchUpInside];
}

- (IBAction)detailAction:(id)sender {
    DHS2_BrandViewController *brandController = [[DHS2_BrandViewController alloc] initWithBrandID:self.brandID];
    brandController.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:brandController animated:YES];
}

- (void)updateCell:(NSDictionary *)dic{
    [self.coverImgView dh_setImageURLString:[dic objectForKey:@"logo"]];
    self.nameLabel.text = [dic objectForKey:@"name"];
    
    self.isLiked = [[dic objectForKey:@"collected"] boolValue];
    if (self.isLiked) {
        [self.likeButton setImage:[UIImage imageNamed:@"product_like"] forState:UIControlStateNormal];
    }else{
        [self.likeButton setImage:[UIImage imageNamed:@"product_unlike"] forState:UIControlStateNormal];
    }
    
    self.dataArray = [dic objectForKey:@"products"];
    [self.collectionView reloadData];
    
    self.brandID = [dic objectForKey:@"_id"];
    self.countLabel.text = [NSString stringWithFormat:@"%lu件服饰",(unsigned long)self.dataArray.count];
}

#pragma mark - UICollectionViewDataSource and UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DHBrandProductCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kDHBrandProductCollectionCell forIndexPath:indexPath];
    [cell uploadCell:self.dataArray[indexPath.row]];
    return cell;
}

#pragma mark ---- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat w = (kScreenWidth - 32) / 3;
    CGFloat h = w * 1.29 + 66;
    return (CGSize){w,h};
}

// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

// 两个cell之间的最小间距，是由API自动计算的，只有当间距小于该值时，cell会进行换行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.dataArray objectAtIndex:indexPath.row];
    DHS2_ProductDetailController *vc = [[DHS2_ProductDetailController alloc] initWithProduct:model];
    vc.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
